package com.lyh.hkcloud.activity;


import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.lyh.hkcloud.R;
import com.lyh.hkcloud.adapter.AutoCompleteTextViewAdapter;
import com.lyh.hkcloud.constants.BusinessConstants;
import com.lyh.hkcloud.constants.HttpUrlComstants;
import com.lyh.hkcloud.module.common.ActivityUtil;
import com.lyh.hkcloud.module.m1300085.service.Service;
import com.lyh.hkcloud.util.base.JsonUtil;
import com.lyh.hkcloud.util.base.ToastUtils;
import com.lyh.hkcloud.util.http.listener.json.JsonFailuredListener;
import com.lyh.hkcloud.util.http.listener.json.JsonSuccessListener;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: wenwenliang
 * @description: 资产信息修改
 * @company—date: 利元数据 2023-11-25 16:28
 **/
public class UpdateFaCardActivity extends AppCompatActivity implements View.OnClickListener,View.OnFocusChangeListener {
    /**
     * 实物码
     */
    private TextView etAssets;

    private Service service;
    /**
     * 位置编号，位置名称
     */
    List<String> locationArray;
    EditText etReceiver;
    private AutoCompleteTextView atvLocationNumber;
    //只创建一次
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //调接口服务
        service = new Service(UpdateFaCardActivity.this);
        //获取布局
        setContentView(R.layout.activity_update_fa_card);
        ImageView goBack = findViewById(R.id.go_back);
        goBack.setOnClickListener(this);
        //获取保存按钮
        Button btSave = findViewById(R.id.bt_save);
        btSave.setOnClickListener(this);
        //实物码
        this.etAssets = findViewById(R.id.et_assets);
        //位置信息下拉框
        this.atvLocationNumber =findViewById(R.id.atv_location_number);
        querySpinner(null, null);
        //修改使用人
        this.etReceiver = findViewById(R.id.et_receiver);
        etReceiver.setOnFocusChangeListener(this);

    }

    //每次启动都会走
    @Override
    protected void onStart() {
        super.onStart();
        Intent intent = getIntent();
        String tvFacardCode = intent.getStringExtra("tvFacardCode");
        etAssets.setText(tvFacardCode);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.go_back:
                //销毁当前页面并返回上一页面
                finish();
                break;
            case R.id.bt_save:
                save();
                break;
        }
        //ToastUtils.makeText((Activity) view.getContext(), "哈哈哈", ToastUtils.LENGTH_SHORT).show();
    }

    /**
     * 保存修改信息
     */
    private void save() {
        //1、修改资产信息

        //2、修改当前盘点的信息
        //3、插入一条资产凭证数据
    }


    /**
     * 查询 位置 下拉数据
     *
     * @param position
     * @param cost
     * @return void
     * @author: zhongfan
     * @Date 2022/2/15 21:00
     **/
    private void querySpinner(String position, String cost) {

        locationArray = new ArrayList<>();
//        costArray = new ArrayList<>();

        /*查询位置信息*/
        service.post(new JSONObject(), HttpUrlComstants.QUERY_LOCATION_INFORMATION, new JsonSuccessListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {

                try {
                    JSONArray centerinfo = JsonUtil.getJsonObjectArray(response, "locationinfo", UpdateFaCardActivity.this);
                    if (centerinfo != null && centerinfo.length() > 0) {
                        for (int i = 0; i < centerinfo.length(); i++) {
                            if (centerinfo.get(i) == null) {
                                continue;
                            }
                            JSONObject jsonObject = new JSONObject(centerinfo.get(i).toString());
                            if (jsonObject.get("location") == null && jsonObject.get("locationname")!= null) {
                                continue;
                            }
                            locationArray.add(jsonObject.optString("location") + BusinessConstants.SEPARATOR +jsonObject.optString("locationname"));
//                            costArray.add(jsonObject.get("locationname").toString());
                        }
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
                //initSpinner(locationArray, spLocationNumber, position, null);
                setATVData(locationArray,atvLocationNumber);
            }
            }, new JsonFailuredListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                Log.e("error",response.optString("message"));
                ToastUtils.makeText(UpdateFaCardActivity.this, msg, ToastUtils.LENGTH_SHORT).show();
            }
        });
    }

    /**
     * 位置信息下拉框设置
     * @param data
     * @param atc
     */
    private void setATVData(List<String>data, AutoCompleteTextView atc){
        AutoCompleteTextViewAdapter actvAdapter =new AutoCompleteTextViewAdapter(this, R.layout.item_select,data);
        atc.setAdapter(actvAdapter);
        atc.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                AutoCompleteTextView view = (AutoCompleteTextView) v;
                if (hasFocus) {
                    view.showDropDown();
                }
            }
        });
        atc.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                AutoCompleteTextView view = (AutoCompleteTextView) v;
                view.showDropDown();
                return false;
            }
        });
    }

    /**
     * 处理键盘是否弹出还是收起
     * @param ev
     * @return
     */
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        //如果是点击事件，获取点击的view，并判断是否要收起键盘
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            //获取目前得到焦点的view
            View v = getCurrentFocus();
            //判断是否要收起并进行处理
            if (ActivityUtil.isShouldHideKeyboard(v, ev)) {
                ActivityUtil.hideKeyboard(v.getWindowToken(), this, etReceiver);
            }
        }
        //这个是activity的事件分发，一定要有，不然就不会有任何的点击事件了
        return super.dispatchTouchEvent(ev);
    }

    /**
     * 焦点事件
     * @param view
     * @param b
     */
    @Override
    public void onFocusChange(View view, boolean b) {

        if (b) { //焦点
            etReceiver.setText("");
            return;
        }

        if (etReceiver.getText() == null || "".equals(etReceiver.getText().toString())) {
            etReceiver.setText("请输入工号！");
            return;
        }

        JSONObject jsonObject = new JSONObject();

        try {
            jsonObject.put("psncode", etReceiver.getText());
            //查询人员信息
            queryPersonnelInformation(jsonObject);
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    /**
     * 通过人员id，查询人员信息
     *
     * @param jsonObject
     * @return void
     * @author: zhongfan
     * @Date 2022/2/15 17:47
     **/
    public void queryPersonnelInformation(JSONObject jsonObject) {
        service.post(jsonObject, HttpUrlComstants.QUERY_PERSONNEL_DETAILS, new JsonSuccessListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                JSONArray data = response.optJSONArray("data");
                JSONObject jsonObject = null;


                try {
                    jsonObject = new JSONObject(data.get(0).toString());
                    JSONArray facardinfo = jsonObject.optJSONArray("psninfo");

                    if (facardinfo == null || facardinfo.length() == 0) {
                        etReceiver.setText("");
                        Toast.makeText(UpdateFaCardActivity.this, "查询人员信息为空", Toast.LENGTH_SHORT).show();
                        return;
                    }

                    jsonObject = new JSONObject(facardinfo.get(0).toString());
                    etReceiver.setText(jsonObject.getString("psncode")+"-"+jsonObject.getString("psnname"));
                    Log.v("jsonObj333333:", jsonObject.toString());

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }, new JsonFailuredListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                Log.e("error",response.optString("message"));
                ToastUtils.makeText(UpdateFaCardActivity.this, msg, ToastUtils.LENGTH_SHORT).show();
            }
        });
    }
}
